package com.qzdsoft.erpcloud.controller.com;

import java.text.ParseException;
import java.util.List;

import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qzdsoft.erpcloud.controller.BaseController;
import com.qzdsoft.erpcloud.domain.basic.DataDictionaryValue;
import com.qzdsoft.erpcloud.domain.com.CustomerSupplierContacts;
import com.qzdsoft.erpcloud.service.basic.DataDictionaryValueService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierContactService;
import com.qzdsoft.erpcloud.service.com.CustomerSupplierService;
import com.qzdsoft.erpcloud.service.sys.Permission;
import com.qzdsoft.erpcloud.service.sys.SysLog;
import com.qzdsoft.erpcloud.service.sys.SysUserService;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierContactsInfo;
import com.qzdsoft.erpcloud.vo.com.CustomerSupplierContactsQueryParam;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.basic.BasicDataDictionaryTypeEnum;

/**
 * 客户的联系人
 * Created by yyb on 2017/7/2 0002.
 */
@Controller
@RequestMapping("/com/customer/supplier/contacts")
public class CustomerSupplierContactController extends BaseController{
	private static final Logger logger = LoggerFactory.getLogger(CustomerSupplierContactController.class);
	private static final String BASEPATH = "com/constacts";
	@Autowired
    private CustomerSupplierContactService comContactService;
    @Autowired
    private CustomerSupplierService supplierCustomerService;
    @Autowired
    private DataDictionaryValueService basicDataDictionaryValueService;
    @Autowired
    private SysUserService sysUserService;
    
    
    @Permission(validate=false)
    @RequestMapping("/index")
    public String index( Model model,Integer id,String companyName,Integer companyId ) {
//        Integer id= Integer.parseInt(request.getParameter("comtomerSupplierId"));
//        logger.debug("id={}",id);
    	model.addAttribute("companyName", companyName);
        model.addAttribute("supplierCustomerId",id);
//        SupplierCustomer comSupplierCustomer =supplierCustomerService.selectById(id);
        model.addAttribute("companyId",companyId);
//        sessionService.saveKey("comtomerSupplierId",id,request);
//        sessionService.saveKey("companyId",comSupplierCustomer.getCompanyId(),request);
        //从数据字典取【职务】的值
        model.addAttribute("positionList",basicDataDictionaryValueService.getByEnumId(BasicDataDictionaryTypeEnum.position.getKey()));
//        return "/com/customer_supplier_contacts";
        return BASEPATH+"/index";
    }
    
    @ResponseBody
    @RequestMapping("/data")
    public LayTableResponse<CustomerSupplierContactsInfo> data(HttpServletRequest request, CustomerSupplierContactsQueryParam param) throws ParseException {

        if(param.isFirst()) {
        	return LayTableResponse.nullDataPage();
        }else{
        	
        	logger.debug("联系人:CompanyId={},CompanyName={},supplierCustomerId={}",param.getCompanyId(),param.getCompanyName(),param.getSupplierCustomerId());
        	return comContactService.selectAll(param);
        }
    }
    /**
     * 打开新增页面
     * @param model
     * @param companyId
     * @param supplierCustomerId
     * @return
     */
    @Permission(validate=false)
    @RequestMapping("/addinit")
    public String addinit(Model model,Integer companyId,Integer supplierCustomerId){
    	//查询职务
    	List<DataDictionaryValue> posts = basicDataDictionaryValueService.getByEnumId(5);
    	model.addAttribute("companyId", companyId);
    	model.addAttribute("supplierCustomerId", supplierCustomerId);
    	model.addAttribute("postList", posts);
    	return BASEPATH+"/add";
    }
    /**
     * 新增保存
     * @param request
     * @param contacts
     * @return
     */
    @SysLog(description="客户联系人新增保存")
    @ResponseBody
    @RequestMapping("/add")
    public Object add(HttpServletRequest request, CustomerSupplierContacts contacts) {
//        contacts.setCompanyId(SessionUtil.getLoggedCompanyId(request, contacts.getCompanyId()));
    	 logger.debug("reamrk={}",contacts.getRemark());
        return comContactService.edit(contacts);
    }
    /**
     * 打开编辑页面
     * @param id
     * @return
     */
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/edit")
    public ModelAndView edit(@RequestParam(value="id")Integer id) {
    	  logger.debug("客户联系人修改:id={}",id);
    	  List<DataDictionaryValue> posts = basicDataDictionaryValueService.getByEnumId(5);
          CustomerSupplierContacts contacts = comContactService.selectById(id);
          ModelAndView mv = new ModelAndView();
          mv.addObject("postList", posts);
          mv.addObject("obj", contacts);
          mv.setViewName(BASEPATH+"/edit");
        return mv;	  
    }
    
    @Permission(validate=false)
    @ResponseBody
    @RequestMapping("/view")
    public ModelAndView view(@RequestParam(value="id")Integer id) {
    	  logger.debug("客户联系人:id={}",id);
    	  List<DataDictionaryValue> posts = basicDataDictionaryValueService.getByEnumId(5);
          CustomerSupplierContacts contacts = comContactService.selectById(id);
          ModelAndView mv = new ModelAndView();
          mv.addObject("postList", posts);
          mv.addObject("obj", contacts);
          mv.setViewName(BASEPATH+"/view");
        return mv;	  
    }
    
    
    /**
     * 修改保存
     * @param request
     * @param contacts
     * @return
     */
    @SysLog(description="客户联系人修改保存")
    @ResponseBody
    @RequestMapping("/edit_execute")
    public Object editExecute(HttpServletRequest request, CustomerSupplierContacts contacts) {
//        contacts.setCompanyId(SessionUtil.getLoggedCompanyId(request, contacts.getCompanyId()));
    	 logger.debug("reamrk={}",contacts.getRemark());
        return comContactService.edit(contacts);
    }
    /**
     * 删除
     * @param request
     * @param id
     * @return
     */
    @SysLog(description="客户联系人删除")
    @ResponseBody
    @RequestMapping("/del_execute")
    public Object delExecute(HttpServletRequest request, @RequestParam("id") Integer id) {
        return comContactService.del(id);
    }
}
